package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import models.Administrador;

import play.db.DB;
import utils.exceptions.BDException;

public class AdministradorDAO {
		
	Connection conexao;
	
	public AdministradorDAO() {		
		conexao = DB.getConnection();
	}
	
	public boolean cadastrar(Administrador adm) throws BDException {
		boolean retorno = false;	
		String comando = "insert into administrador (nome, cpf, login, senha)"+
		"values(?,?,?,?)";
	
		try {
			PreparedStatement p = this.conexao.prepareStatement(comando);
			
			p.setString(1, adm.getNome());
			p.setLong(2, adm.getCpf());
			p.setString(3, adm.getLogin());
			p.setString(4, adm.getSenha());
			
			p.execute();			
			retorno = true;
		} catch (SQLException e) {
			throw new BDException("Ocorreu erro na hora de CADASTRAR a Adm (try catch)",e.getStackTrace());
		}
		
		return retorno;
	}

	public boolean editar(Administrador adm) throws BDException{
		String comando = "update administrador set nome=?, login=?, senha=? where cpf=?";
		
		try {
			PreparedStatement p = this.conexao.prepareStatement(comando);
			
			p.setString(1, adm.getNome());		
			p.setString(2, adm.getLogin());
			p.setString(3, adm.getSenha());
			p.setLong(4, adm.getCpf());
			
			p.execute();
			
			return true;
		} catch (SQLException e) {
			throw new BDException("Ocorreu erro na hora de EDITAR a Adm (try catch)",e.getStackTrace());
		}
			
	}

	public List<Administrador> listarTodos() throws BDException {
		List<Administrador> retorno = new ArrayList<Administrador>();
		
		String comando = "select nome, login, cpf from administrador";
		
		try {
			java.sql.Statement start = conexao.createStatement();
			ResultSet rs = start.executeQuery(comando);
			
			while(rs.next()){
				Administrador adm = new Administrador();	
				adm.setNome(rs.getString("nome"));
				adm.setLogin(rs.getString("login"));
				adm.setCpf(rs.getLong("cpf"));
			
				retorno.add(adm);
			}
		} catch (SQLException e) {
			throw new BDException("Ocorreu erro na hora de LISTAR a Adm (try catch)",e.getStackTrace());
		}
		
		return retorno;
	}

	public boolean excluir(long cpf) throws BDException {
		String comando1 = "delete from administrador where cpf=?";		
		try {			
			PreparedStatement ps1 = this.conexao.prepareStatement(comando1);			
			ps1.setLong(1, cpf);			
			ps1.execute();			
			return true;
			
		} catch (SQLException e) {
			throw new BDException("Ocorreu erro na hora de EXCLUIR a Adm (try catch)",e.getStackTrace());
		}
	}

	public Administrador buscarAdm(long cpf) throws BDException {		
		Administrador adm = new Administrador();		
		String comando = "select * from administrador where cpf = '"+cpf+"'";				
		try {
			java.sql.Statement start = conexao.createStatement();
			ResultSet rs = start.executeQuery(comando);			
			while(rs.next()){	
				adm.setNome(rs.getString("nome"));
				adm.setCpf(rs.getLong("cpf"));
				adm.setLogin(rs.getString("login"));
				adm.setSenha(rs.getString("senha"));						
			}
		} catch (SQLException e) {
			throw new BDException("Ocorreu erro na hora de BUSCAR a Adm (try catch)",e.getStackTrace());
		}		
		return adm;
	}
	
	public Administrador buscarAdmLogin(String login, String senha) throws BDException {		
		Administrador adm = new Administrador();		
		String comando = "select * from administrador where login = '"+login+"' and senha = '"+senha+"'";				
		try {
			java.sql.Statement start = conexao.createStatement();
			ResultSet rs = start.executeQuery(comando);			
			while(rs.next()){	
				adm.setNome(rs.getString("nome"));
				adm.setCpf(rs.getLong("cpf"));
				adm.setLogin(rs.getString("login"));
				adm.setSenha(rs.getString("senha"));						
			}			
		} catch (SQLException e) {
			throw new BDException("Ocorreu erro na hora de BUSCAR a Adm (try catch)",e.getStackTrace());
		}		
		return adm;
	}
	
	
}
